from django.db import models
import datetime

# Create your models here.
class Orders(models.Model):
    """
    用户订单表
    """
    ORDER_STATUS = (
        ("TRADE_CLOSED", "超时关闭"),
        ("TRADE_FINISHED", "交易完成"),
        ("paying", "待支付"),
        ("taking", "待接单"),
        ("going", "进行中"),
    )
    PAY_TYPE = (
        ("alipay", "支付宝"),
        ("wechat", "微信"),
    )

    # 关联用户
    users = models.ForeignKey('users.Users', on_delete=models.CASCADE)
    # 关联站点
    station = models.ForeignKey('station.Station', on_delete=models.CASCADE)
    # 关联骑手，骑手接单后显示
    rider = models.ForeignKey('rider.Rider', on_delete=models.CASCADE, null=True)
    # 订单号唯一
    order_sn = models.CharField(verbose_name="订单编号", max_length=30, null=True, blank=True, unique=True)
    # 微信支付会用到
    nonce_str = models.CharField(verbose_name="随机加密串", max_length=50, null=True, blank=True, unique=True)
    # 支付宝交易号
    trade_no = models.CharField(verbose_name="交易号", max_length=100, unique=True, null=True, blank=True)
    # 支付状态
    pay_status = models.CharField(verbose_name="订单状态", choices=ORDER_STATUS, default="paying", max_length=30)
    # 订单的支付类型
    pay_type = models.CharField(verbose_name="支付类型", choices=PAY_TYPE, default="alipay", max_length=10)
    post_script = models.CharField(verbose_name="订单留言", max_length=200, null=True, blank=True)
    order_mount = models.FloatField(verbose_name="订单金额", default=0.0)
    pay_time = models.DateTimeField(verbose_name="支付时间", null=True, blank=True)
    # 用户信息
    pickup_code = models.CharField(verbose_name='取货码', max_length=8, null=True, default="无")
    address = models.CharField("收货地址", max_length=100, default="")
    signer_name = models.CharField("签收人", max_length=20, default="")
    singer_mobile = models.CharField("联系电话", max_length=11)
    add_time = models.DateTimeField(verbose_name="添加时间", auto_now=True)

    def __str__(self):
        return self.order_sn

class ExpressInfo(models.Model):
    """
    单号信息表
    """
    EXPRESS_STATUS = (
        ('expressing', '派送中'),
        ('finished', '已完成'),
    )

    express_no = models.CharField(max_length=20, verbose_name='快递单号', null=False)
    express_status = models.CharField(verbose_name='单号状态', choices=EXPRESS_STATUS, max_length=20, default='expressing')
    arrival_time = models.DateTimeField(verbose_name='送到时间', null=True, default=(datetime.datetime.now()+datetime.timedelta(hours=3)).strftime("%Y-%m-%d %H:%M:%S"))
    orders = models.ForeignKey('trade.Orders', related_name='expressInfo', on_delete=models.CASCADE)
